package com.example.a86186.apps.util;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

import com.example.a86186.apps.MainActivity;

import java.util.List;

/**
 * Created by Administrator on 2018/4/17.
 * 获取用户的地理位置
 */
public final class GPSUtils {

    private static GPSUtils instance;
    private android.app.Activity mContext;
    private LocationManager locationManager;

//    private LocationListener mOnLocationListener;

    private GPSUtils(Activity context) {
        this.mContext = context;
    }

    public static GPSUtils getInstance(Activity context) {
        if (instance == null) {
            instance = new GPSUtils(context);
        }
        return instance;
    }

    /**
     * 获取经纬度
     *
     * @return
     */
    public String getLngAndLat( LocationListener socketLocationListener) {

        String locationProvider = null;
        locationManager = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE);
        //获取所有可用的位置提供器
        List<String> providers = locationManager.getProviders(true);

        if (providers.contains(LocationManager.GPS_PROVIDER)) {
            //如果是GPS
            locationProvider = LocationManager.GPS_PROVIDER;
        } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
            //如果是Network
            locationProvider = LocationManager.NETWORK_PROVIDER;
        } else {
            Intent i = new Intent();
            i.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(i);
            return null;
        }

        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION}, 100);
            return null;
        }
        Location location = locationManager.getLastKnownLocation(locationProvider);
        if (location != null) {
            //不为空,显示地理位置经纬度
            System.out.println("99999999999");
            socketLocationListener.onLocationChanged(location);
            System.out.println("99999999999");
        }
        //监视地理位置变化
        locationManager.requestLocationUpdates(locationProvider, 3000, 1, socketLocationListener);
        return null;
    }



}


//76.27447509765625     40.14786917
//80.6529541015625     40.14771268
//57.66998291015625     40.14774428

  /*  public LocationListener locationListener = new LocationListener() {

        // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        // Provider被enable时触发此函数，比如GPS被打开
        @Override
        public void onProviderEnabled(String provider) {
            System.out.println(provider +"  onProviderEnabled");
        }

        // Provider被disable时触发此函数，比如GPS被关闭
        @Override
        public void onProviderDisabled(String provider) {

            System.out.println(provider +"  onProviderDisabled");

        }
        //当坐标改变时触发此函数，如果Provider传进相同的坐标，它就不会被触发
        @Override
        public void onLocationChanged(Location location) {
            *//*if (mOnLocationListener != null) {
                mOnLocationListener.OnLocationChange(location);
            }*//*
            double altitude = location.getAltitude();
            double latitude = location.getLatitude();
            System.out.println(altitude+"     "+latitude);
            String lo = location.getLatitude() + "  " + location.getAltitude();
            System.out.println("----------");

        }
    };*/